package cn.swing.his.user.webapi.service.impl;


import cn.swing.his.pojo.user.dto.ChangeRoleOperationDTO;

import cn.swing.his.pojo.user.dto.DoctorDetailsDTO;
import cn.swing.his.pojo.user.dto.PatientOrderDTO;
import cn.swing.his.pojo.user.dto.PatientRegisterDTO;
import cn.swing.his.pojo.user.entity.MenuEntity;
import cn.swing.his.pojo.user.vo.*;
import cn.swing.his.user.service.IUserService;
import cn.swing.his.user.webapi.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@DubboService
public class UserServiceImpl implements IUserService {


    @Autowired
    UserMapper userMapper;


    /**
     * 返回所有患者列表信息
     *
     * @return
     */
    @Override
    public List<PatientListVO> patientList() {
        List<PatientListVO> list = userMapper.patientList();
        log.info("用户列表查询详情为：{}", list);
        return list;
    }


    /**
     * 返回所有医生列表信息
     *
     * @return
     */
    @Override
    public List<DoctorListVO> doctorList(Integer roomId) {
        return userMapper.doctorList(roomId);
    }


    /**
     * 根据医生ID删除单个医生信息
     *
     * @return
     */
    @Override
    public void deleteSimpleDoctorById(Integer id) {
        userMapper.deleteSimpleDoctorById(id);
    }

    /**
     * 插入单个医生信息
     *
     * @return
     */
    @Override
    public void insertSimpleDoctor(DoctorDetailsDTO doctorDetailsDTO) {
        userMapper.insertSimpleDoctor(doctorDetailsDTO);
        int doctorMaxId = userMapper.selectDoctorMaxId();
        userMapper.insertDoctorRole(doctorMaxId, doctorDetailsDTO.getRoleId());
    }

    /**
     * 根据医生ID更新医生信息
     *
     * @param doctorDetailsDTO
     */
    @Override
    public void updateSimpleDoctorDetailsById(DoctorDetailsDTO doctorDetailsDTO) {
        userMapper.updateSimpleDoctorDetailsById(doctorDetailsDTO);
        userMapper.deleteDoctorRole(doctorDetailsDTO.getId());
        userMapper.insertDoctorRole(doctorDetailsDTO.getId(), doctorDetailsDTO.getRoleId());
    }

    /**
     * 插入病人预约情况信息
     *
     * @param patientOrderDTO
     */
    @Override
    public void insertPatientOrder(PatientOrderDTO patientOrderDTO) {
        userMapper.insertPatientOrder(patientOrderDTO);
    }


    /**
     * 根据医生身份证号获取单个医生所有个人信息
     *
     * @param idCard
     * @return
     */
    @Override
    public DoctorDetailsVO selectSimpleDoctorDetails(String idCard) {
        return userMapper.selectSimpleDoctorDetails(idCard);
    }


    /**
     * 根据病人身份证号获取单个病人所有个人信息
     *
     * @param idCard
     * @return
     */
    @Override
    public PatientDetailsVO selectSimplePatientDetails(String idCard) {
        return userMapper.selectSimplePatientDetails(idCard);
    }

    /**
     * 根据患者身份证号查询个人病历单
     *
     * @param idCard
     * @return
     */
    @Override
    public List<MedicalRecordVO> selectMedicalRecordList(String idCard) {
        return userMapper.selectMedicalRecordList(idCard);
    }


    /**
     * 返回科室中所有列表信息
     *
     * @return
     */
    @Override
    public List<RoomListVO> selectRoomList() {
        return userMapper.selectRoomList();
    }


    /**
     * 根据患者ID删除单个患者信息
     *
     * @return
     */
    @Override
    public void deleteSimplePatientById(Integer id) {
        userMapper.deleteSimplePatientById(id);
    }

    /**
     * 查询出所有角色列表信息
     *
     * @return
     */
    @Override
    public List<RoleListVO> selectRoleList() {
        return userMapper.selectRoleList();
    }

    /**
     * 插入单个角色信息
     *
     * @param name
     */
    @Override
    public void insertRole(String name) {
        userMapper.insertRole(name);
    }

    /**
     * 根据角色ID删除单个角色信息
     *
     * @param id
     */
    @Override
    public void deleteRoleById(Integer id) {
        userMapper.deleteRoleById(id);

    }

    /**
     * 查询出所有权限列表信息
     *
     * @return
     */
    @Override
    public List<PermissionListVO> selectPermissionList() {
        return userMapper.selectPermissionList();
    }

    /**
     * 根据ID查询医生姓名
     *
     * @param id
     * @return
     */
    @Override
    public DoctorSimpleVO selectDoctorNameById(Integer id) {
        return userMapper.selectDoctorNameById(id);
    }


    /**
     * 根据ID批量删除病人列表信息
     *
     * @param ids
     */
    @Override
    public void deleteDoctorsByIds(Integer[] ids) {
        userMapper.deleteDoctorsByIds(ids);
    }


    /**
     * 根据ID批量删除病人列表信息
     *
     * @param ids
     */
    @Override
    public void deletePatientsByIds(Integer[] ids) {
        userMapper.deletePatientsByIds(ids);
    }

    /**
     * 根据ID修改医生个人密码
     *
     * @param id
     */
    @Override
    public void updateDoctorPasswordById(Integer id, String password) {
        userMapper.updateDoctorPasswordById(id, password);
    }

    /**
     * 根据病人ID返回病人预约信息
     *
     * @param id
     * @return
     */
    @Override
    public List<OrderDetailsVO> orderDetailsById(Integer id) {
        List<OrderDetailsVO> orderDetailsVOS = userMapper.orderDetailsById(id);
        return orderDetailsVOS;
    }


    /**
     * 根据角色ID获取该角色所有权限信息
     *
     * @param id
     * @return
     */
    @Override
    public List<PermissionListVO> permissionListById(Integer id) {
        List<PermissionListVO> permissionsById = userMapper.permissionListById(id);
        return permissionsById;
    }


    /**
     * 更改角色可以使用的操作
     *
     * @param changeRoleOperations
     * @return
     */
    @Override
    public int changeRoleOperation(List<ChangeRoleOperationDTO> changeRoleOperations) {
        if (changeRoleOperations.size() == 1) {
            userMapper.deleteRolePermissionNull();
            int change = userMapper.deleteRoleOperationById(changeRoleOperations.get(1).getRoleId());
            userMapper.deleteRolePermissionNull();
            return 0;
        }
        int change = userMapper.deleteRoleOperationById(changeRoleOperations.get(1).getRoleId());
        userMapper.deleteRolePermissionNull();
        return userMapper.changeRoleOperation(changeRoleOperations);
    }

    /**
     * 患者注册
     *
     * @param patientRegisterDTO
     */
    @Override
    public void registerPatient(PatientRegisterDTO patientRegisterDTO) {
        patientRegisterDTO.setAvatar("https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png");
        patientRegisterDTO.setIdCardImage("无");
        patientRegisterDTO.setDescription("暂无描述");
        //设置当前挂号状态 0未挂号
        patientRegisterDTO.setState(0);
        //将明文密码设置为加密状态
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        patientRegisterDTO.setPassword(passwordEncoder.encode(patientRegisterDTO.getPassword()));
        userMapper.registerPatient(patientRegisterDTO);
    }


    /**
     * 根据医生id和菜单级别查看不同角色下不同的导航栏菜单
     *
     * @param id
     * @return
     */
    @Override
    public List<DoctorMenuVO> selectDoctorMenuById(Integer id) {
        // 获取父层级
        List<DoctorMenuVO> list = new ArrayList<>();
        List<DoctorMenuVO> temp = userMapper.selectDoctorMenuById(5);
        for (DoctorMenuVO t : temp) {
            if (t != null) {
                if (t.getDeep().equals("0")) {
                    list.add(t);
                }
            }
        }
        // 给父层级添加子层级
        for (DoctorMenuVO d : list) {
            List<MenuEntity> children = new ArrayList<>();
            for (DoctorMenuVO t : temp) {
                if (t != null) {
                    if (d.getMenuType().equals(t.getMenuType()) && t.getDeep().equals("1")) {
                        MenuEntity m = new MenuEntity();
                        m.setId(t.getId());
                        m.setMenuType(t.getMenuType());
                        m.setIcon(t.getIcon());
                        m.setMenuName(t.getMenuName());
                        m.setPath(t.getPath());
                        m.setDeep(t.getDeep());
                        children.add(m);
                    }
                }
            }
            d.setChildren(children);
        }
        System.out.println("list = " + list);
        return list;
    }

    /**
     * 根据科室ID查询单个科室详细信息
     *
     * @param id
     * @return
     */
    @Override
    public RoomDetailsVO selectRoomDetailById(Integer id) {
        return userMapper.selectRoomDetailById(id);
    }
}
